Ext.ns('TaxonTagger');

Ext.getUrlParam = function(param) {
	var params = Ext.urlDecode(location.search.substring(1));
	return param ? params[param] : params;
};

TaxonTagger.defaultDocumentSettings = {
		info: {
				author: ""
			,	date: ""
			,	publisher: ""
			,	title: ""
			,	issn: ""
			,	isbn: ""
			,	doi: ""
			,	sici: ""
			,	url: ""
		}
	,	customWords: []
	,	removedWords: []
	,	comments: new Object()
}

TaxonTagger.docStore  = new Ext.ux.data.PagingJsonStore({
		lastOptions: {params: {start: 0, limit: 29}}	
	,	fields: [
				{name: 'taxonID', mapping: 'taxonID'}
			,	{name: 'scientificName', mapping: 'scientificName'}
			,	{name: 'genus', mapping: 'genus'}
			,	{name: 'family', mapping: 'family'}
			,	{name: 'class', mapping: 'class'}
			,	{name: 'class_', mapping: 'class_'} // added after classify
			,	{name: 'order', mapping: 'order'}
		//	,	{name: 'datasetName', mapping: 'datasetName'}
		//	,	{name: 'higherTaxon', mapping: 'higherTaxon'}
		//	,	{name: 'acceptedName ', mapping: 'acceptedName'}
		//	,	{name: 'accordingTo ', mapping: 'accordingTo'}
			,	{name: 'phylum', mapping: 'phylum'}
			,	{name: 'kingdom', mapping: 'kingdom'}
			,	{name: 'processed', mapping: 'processed'}
			,	{name: 'found', mapping: 'found'}
			,	{name: 'isSynonym', mapping: 'isSynonym'}
			/** Used to load data in grid panel*/
			,	{name: 'offset', mapping: 'offset'} 
			,	{name: 'orig', mapping: 'orig'}   
			,	{name: 'full', mapping: 'full'}
			,	{name: 'source', mapping: 'source'}
			,	{name: 'verbatim', mapping: 'verbatim'}
			,	{name: 'comment', mapping: 'comment'}
			,	{name: 'frequency', mapping: 'frequency'}
			,	{name: 'pro', mapping: 'pro'}
		]
});

TaxonTagger.saveStorage = function() {
	localStorage.setItem("url-" + TaxonTagger.currentDocument.info.url, Ext.encode(TaxonTagger.currentDocument) );
}

TaxonTagger.loadStorage = function(mif, url){
	if (url != '') {
		TaxonTagger.currentDocument = Ext.decode( localStorage.getItem("url-" + url) ) || TaxonTagger.defaultDocumentSettings;
		TaxonTagger.currentDocument.info.url = url;
		Ext.getCmp('tools').enableChild();
		this.docinfoPanel.loadDocData();
		this.enable();
		var tConfig = Ext.decode(localStorage.getItem("settings")) || Config;
		Ext.getCmp('nubPanel').primaryCombo.setValue(tConfig["general"]["primary"]);
		Ext.getCmp('nubPanel').secondaryCombo.setValue(tConfig["general"]["secondary"]);
		Ext.getCmp('nubPanel').showChild();
	}
}

Ext.onReady(function() {

	Ext.QuickTips.init();
	lang = Ext.getUrlParam('lang');
	if(!lang){
		lang = 'en';
	};
	
	Ext.fly(document.body).on('contextmenu', function(e, target) {
		e.preventDefault();
	});
	
	var viewport = new TaxonTagger.mainViewport();
	doc = Ext.getUrlParam('document');
	if (doc) {
		TaxonTagger.currentDocument = Ext.decode(doc);
		TaxonTagger.saveStorage();
		viewport.taggerpanel.urlCombo.setValue(TaxonTagger.currentDocument.info.url);
		viewport.htw.urlCombo.setValue(TaxonTagger.currentDocument.info.url);
		viewport.taggerpanel.loadDocument(TaxonTagger.currentDocument.info.url);		
	};
	
	Ext.get('loading').remove();
	Ext.get('loading-mask').fadeOut({remove:true});

});